package com.example.pachong.controller;

import com.example.pachong.pojo.Discuss;
import com.example.pachong.pojo.Ip;
import com.example.pachong.pojo.Users;
import com.example.pachong.pojo.comments;
import com.example.pachong.service.CommentService;
import com.example.pachong.service.DiscussService;
import com.example.pachong.service.IpService;
import com.example.pachong.service.UserService;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/pachong")
public class PachongController {
    @Autowired
    private DiscussService discussService;
    @Autowired
    private UserService userService;
    @Autowired
    private IpService ipService;
    @Autowired
    private CommentService commentService;

    int i = 1;

    //使用jsoup加请求头
    @GetMapping("/jsoupHeader")
    public void jsoupHeader() throws IOException {
        //String url = "https://bbs-api.miyoushe.com/post/wapi/userPost?offset=55249915&size=10&uid=391147034";
        // String url = "https://mp.weixin.qq.com/s/YPrqMOYYrAtCni2VT8c4jA";
        //Document document = Jsoup.parse(new URL(url), 10000);
        Connection connect = Jsoup.connect("https://bbs-api.miyoushe.com/post/wapi/userPost?offset=55249915&size=10&uid=391147034");
        //设置多个请求头，头信息保存到Map集合中
        Map<String, String> header = new HashMap<String, String>();
        header.put("Host", "bbs-api.miyoushe.com");
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0");
        header.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
        header.put("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        header.put("Accept-Encoding", "gzip, deflate, br, zstd");
        header.put("Cache-Control", "no-cache");
        header.put("Connection", "keep-alive");
        header.put("cookie", "_MHYUUID=854f8953-a7df-438f-81d9-dc32f84d4a44; DEVICEFP_SEED_ID=8845fe6b87a20d05; DEVICEFP_SEED_TIME=1723053151120; _ga=GA1.1.601156800.1723053151; DEVICEFP=38d7fca8229de; _ga_KS4J8TXSHQ=GS1.1.1723212938.4.1.1723214160.0.0.0; acw_tc=0a32503917232151905613676e2aa570adca667897662f3944d58643654803");
        Connection conheader = connect.headers(header);
        Document document = conheader.get();
        //Document document = Jsoup.connect(url).get();
        Gson gson = new Gson();
        String jsonString = gson.toJson(document.toString());
        OutputStreamWriter osw = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("C:\\Users\\jizhaoyang\\Desktop\\text.txt")));
        osw.write(jsonString,0,jsonString.length());
        osw.close();

        Elements content = document.select("div.reply-card");
        //Elements content = document.select("div.mhy-article-page__title");
        System.out.println(content.size());
        for(Element element:content){
            System.out.println(element.select("h1").first().text());
        }
        //Element content = document.getElementById("js_content");
        Element imgs = content.get(1);
        System.out.println(content);
        System.out.println(content);
        for(Element element:content){
            String id = element.select("div.reply-card__container").select("div.reply-card__header").select("div.reply-card__account")
                    .select("a.mhy-router-link mhy-account-title reply-card__nickname").select("span.mhy-account-title__name").first().text();
            System.out.println(id);
        }
    }
    //构造一个webClient 模拟Chrome 浏览器
    @GetMapping("/webClient")
    public void webClient(){
        //String title="https://www.miyoushe.com/ys/article/55249915";
        String title = "https://mp.weixin.qq.com/s/YPrqMOYYrAtCni2VT8c4jA";
        Document doc = null;
        try {
            //构造一个webClient 模拟Chrome 浏览器
            WebClient webClient = new WebClient(BrowserVersion.CHROME);
            //屏蔽日志信息
            LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
            java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
            //支持JavaScript
            webClient.getOptions().setJavaScriptEnabled(true);
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setActiveXNative(false);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.setAjaxController(new NicelyResynchronizingAjaxController());//很重要，设置支持AJAX
            //webClient.getOptions().setTimeout(5000);
            HtmlPage rootPage = webClient.getPage(title);
            //设置一个运行JavaScript的时间
            webClient.waitForBackgroundJavaScript(30000);//异步JS执行需要耗时,所以这里线程要阻塞30秒,等待异步JS执行结束
            String html = rootPage.asXml();
            doc = Jsoup.parse(html);
            String filePath = "C:\\Users\\jizhaoyang\\Desktop\\text.txt";
            writeFile(doc.toString(),filePath);
            System.out.println("success");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    //使用json
    @GetMapping("/jsonApiFile")
    public void jsonApiFile() throws IOException, InterruptedException {
        //评论api https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=20&post_id=56356803&size=20
        //个人用户api https://bbs-api.miyoushe.com/post/wapi/userPost?size=10&uid=391147034  OR  https://bbs-api.miyoushe.com/post/wapi/userPost?offset=55249915&size=10&uid=391147034
        //评论url 上一次https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=20&post_id=56393124&size=20
        //       下一次 https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=40&post_id=56393124&size=20
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=false&last_id=0&order_type=2&post_id=56283773&size=20"))
                .header("Accept", "application/json;charset=utf-8")  // 修改为期望的 JSON 类型\
                .header("cookie", "_MHYUUID=854f8953-a7df-438f-81d9-dc32f84d4a44; DEVICEFP_SEED_ID=8845fe6b87a20d05; DEVICEFP_SEED_TIME=1723053151120; _ga=GA1.1.601156800.1723053151; DEVICEFP=38d7fca8229de; _ga_KS4J8TXSHQ=GS1.1.1723212938.4.1.1723214160.0.0.0; acw_tc=0a32503917232151905613676e2aa570adca667897662f3944d58643654803")
                .GET()
                .build();


        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
        String responseBody = response.body();

        boolean islast = true;
        String start = "";
        String before = "\"list\"";
        String lasttemp = "\"is_last\"";
        int lastIndex = responseBody.lastIndexOf(lasttemp) + lasttemp.length(); // 跳过"content"字符串
        int nextlastIndex = responseBody.indexOf(",", lastIndex + 1); // 查找第一个逗号
        if (nextlastIndex != -1) {
            // 提取之间的内容
            String contentValue = responseBody.substring(lastIndex + 1, nextlastIndex);
            islast= !contentValue.equals("false");
        }

        String lastid = "\"last_id\"";
        int lastidIndex = responseBody.lastIndexOf(lastid) + lastid.length(); // 跳过"last_id"字符串
        int quoteIndex = responseBody.indexOf("\"", lastidIndex); // 查找第一个双引号
        if (quoteIndex != -1) {
            int secondQuoteIndex = responseBody.indexOf("\"", quoteIndex + 1); // 查找第二个双引号
            if (secondQuoteIndex != -1) {
                // 提取两个双引号之间的内容
                start = responseBody.substring(quoteIndex + 1, secondQuoteIndex);
                System.out.println("last_id: "+start);
            }
        }
        int beforeIndex = responseBody.lastIndexOf(before) + before.length();
        System.out.println(beforeIndex+" "+(lastidIndex-lastid.length())+" "+responseBody.charAt(beforeIndex + 1)+" "+responseBody.charAt(lastidIndex-lastid.length()));
        responseBody = responseBody.substring(beforeIndex + 1,lastidIndex - lastid.length() - 1);
        System.out.println(islast+" "+start);
        String filePath = "C:\\Users\\jizhaoyang\\Desktop\\file.txt";
        writeFile(responseBody,filePath);
        printByGson(responseBody,"56283773");
        System.out.println("success");
    }
    //正则表达式实验
    @GetMapping("/zhengze")
    public void zhengze(){
        String jsonFragment = "\"game_id\": 2,\n" +
                "\"post_id\": \"55010712\",\n" +
                "\"f_forum_id\": 26,\n" +
                "\"uid\": \"391147034\",\n" +
                "\"subject\": \"水\",\n" +
                "\"content\": \"突发奇想想搞个梯子，去谷歌商店下载中 外国人性别是真的自由 二编 （此处应是注册谷歌账号时性别选项，但图片不让发。反正很自由） [图片]  \",\n" +
                "\"cover\": \"\",\n" +
                "\"view_type\": 1,\n" +
                "\"created_at\": 1720724699,\n" +
                "\"images\": [...]";

        // 正则表达式匹配"content"字段及其值
        String regex = "\"content\": \"(.*?)\"";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(jsonFragment);

        if (matcher.find()) {
            // 提取并打印content字段的值
            System.out.println("Content: " + matcher.group(1));
        } else {
            System.out.println("Content field not found.");
        }
    }
    //输出选择内容
    @GetMapping("/shuchuContent")
    public void shuchuContent() throws IOException {
        List<String> contentList = new ArrayList<>();
        //查找关键字
        String temp = "\"reply\"";
        String stoptemp = "\"stat\"";
        String line;
        line = readFile();
        String lasttemp = "\"is_last\"";

        while (line.contains(temp)) {
            int startIndex = line.lastIndexOf(temp) + temp.length(); // 跳过"content"字符串
//            int stopIndex = line.lastIndexOf(stoptemp); // 跳过"content"字符串
            int stopIndex = line.indexOf(stoptemp, startIndex); // 查找第一个"stat"
            String contentValue = line.substring(startIndex - temp.length(), stopIndex);
            contentList.add(contentValue);
            line = line.substring(0,startIndex - temp.length() - 2);
        }
        List<String> compares = new ArrayList<String>();
        compares.add("\"ip_region\"");
        compares.add("\"level\"");
        compares.add("\"nickname\"");
        compares.add("\"created_at\"");
        compares.add("\"content\"");
        compares.add("\"uid\"");
        for(String str : contentList){
            List<String> discuss = new ArrayList<>();
            for(String temp1:compares) {
                if (str != null && str.contains(temp1)) {
                    int startIndex = str.lastIndexOf(temp1) + temp1.length(); // 跳过"content"字符串
                    if(temp1.equals("\"level\"") || temp1.equals("\"created_at\"")){
                        int secondQuoteIndex = str.indexOf(",", startIndex + 1); // 查找第二个双引号
                        if (secondQuoteIndex != -1) {
                            // 提取两个双引号之间的内容
                            String contentValue = str.substring(startIndex + 1, secondQuoteIndex);
                            //String contentValue = str.substring(startIndex - temp1.length(), secondQuoteIndex);
                            discuss.add(contentValue);
                        }
                    }else {
                        int quoteIndex = str.indexOf("\"", startIndex); // 查找第一个双引号
                        if (quoteIndex != -1) {
                            int secondQuoteIndex = str.indexOf("\"", quoteIndex + 1); // 查找第二个双引号
                            if (secondQuoteIndex != -1) {
                                // 提取两个双引号之间的内容
                                String contentValue = str.substring(quoteIndex + 1, secondQuoteIndex);
                                //String contentValue = str.substring(startIndex - temp1.length(), secondQuoteIndex);
                                discuss.add(contentValue);
                            }
                        }
                    }
                    str = str.substring(0, startIndex - temp1.length() - 2);
                }
            }
            Users user = new Users();
            Ip ip = new Ip();
            Discuss discuss1 = new Discuss();

            ip.setIpcol(discuss.get(0));
            ip.setIdip(discuss.get(5));

            user.setLevel(Integer.parseInt(discuss.get(1)));
            user.setNickname(discuss.get(2));
            user.setUid(discuss.get(5));
            user.setFinallytime(discuss.get(3));

            discuss1.setDiscusscol(discuss.get(4));
            discuss1.setIddis(discuss.get(5));
            discuss1.setTime(discuss.get(3));

            System.out.println(user);
            System.out.println(ip);
            System.out.println(discuss1);
            System.out.println();
            userService.tianjia(user);
            ipService.tianjia(ip);
            discussService.tianjia(discuss1);
        }
    }
    //读文件
    public String readFile() throws IOException {
        String filePath = "C:\\Users\\jizhaoyang\\Desktop\\text.txt";
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            StringBuilder temp = new StringBuilder();
            String line;
            while((line = br.readLine()) != null){
                temp.append(line);
            }
            br.close();
            return temp.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    //写文件
    public void writeFile(String response,String filePath){
        try (var os = new java.io.FileOutputStream(filePath,true);
             var writer = new java.io.BufferedWriter(new java.io.OutputStreamWriter(os, StandardCharsets.UTF_8))) {
            writer.write(response);
        }catch(Exception e){
            e.printStackTrace();
        }

    }
    //使用json配合输出
    @GetMapping("/jsonApi")
    public void jsonApi() throws IOException, InterruptedException {
        //评论api https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=20&post_id=56356803&size=20
        //个人用户api https://bbs-api.miyoushe.com/post/wapi/userPost?size=10&uid=391147034  OR  https://bbs-api.miyoushe.com/post/wapi/userPost?offset=55249915&size=10&uid=391147034
        //评论url 上一次https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=20&post_id=56393124&size=20
        //       下一次 https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=40&post_id=56393124&size=20
        //   https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=60&post_id=56232502&size=20
        //   https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=40&post_id=55489190&size=20
        //最新 https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=false&order_type=2&post_id=56427717&size=20

        boolean islast = false;
        String before = "\"list\"";
        String start = "0";
        String size = "40";
        String postid = "55221094";
//        while(!islast) {
//            String url = "https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id="+start+"&post_id="+postid+"&size="+size;
//            HttpClient client = HttpClient.newHttpClient();
//            HttpRequest request = HttpRequest.newBuilder()
//                    .uri(URI.create(url))
//                    .header("Accept", "application/json;charset=utf-8")  // 修改为期望的 JSON 类型\
//                    .header("cookie", "_MHYUUID=854f8953-a7df-438f-81d9-dc32f84d4a44; DEVICEFP_SEED_ID=8845fe6b87a20d05; DEVICEFP_SEED_TIME=1723053151120; _ga=GA1.1.601156800.1723053151; DEVICEFP=38d7fca8229de; _ga_KS4J8TXSHQ=GS1.1.1723212938.4.1.1723214160.0.0.0; acw_tc=0a32503917232151905613676e2aa570adca667897662f3944d58643654803")
//                    .GET()
//                    .build();
//
//
//            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
//            String responseBody = response.body();
//
//            String lasttemp = "\"is_last\"";
//            int lastIndex = responseBody.lastIndexOf(lasttemp) + lasttemp.length(); // 跳过"content"字符串
//            int nextlastIndex = responseBody.indexOf(",", lastIndex + 1); // 查找第一个逗号
//            if (nextlastIndex != -1) {
//                // 提取之间的内容
//                String contentValue = responseBody.substring(lastIndex + 1, nextlastIndex);
//                islast= !contentValue.equals("false");
//            }
//
//            String lastid = "\"last_id\"";
//            int lastidIndex = responseBody.lastIndexOf(lastid) + lastid.length(); // 跳过"last_id"字符串
//            int quoteIndex = responseBody.indexOf("\"", lastidIndex); // 查找第一个双引号
//            if (quoteIndex != -1) {
//                int secondQuoteIndex = responseBody.indexOf("\"", quoteIndex + 1); // 查找第二个双引号
//                if (secondQuoteIndex != -1) {
//                    // 提取两个双引号之间的内容
//                    start = responseBody.substring(quoteIndex + 1, secondQuoteIndex);
//                    writeFile("first--last_id: "+start+" "+islast+"\n","C:\\Users\\jizhaoyang\\Desktop\\text.txt");
//                }
//            }
//            int beforeIndex = responseBody.lastIndexOf(before) + before.length();
//            //System.out.println(beforeIndex+" "+(lastidIndex-lastid.length())+" "+responseBody.charAt(beforeIndex + 1)+" "+responseBody.charAt(lastidIndex-lastid.length()));
//            responseBody = responseBody.substring(beforeIndex + 1,lastidIndex - lastid.length() - 1);
//
//            printByGson(responseBody,postid);
//            //printContent(responseBody,postid);
//            //findindex(responseBody);
//            Random r = new Random();
//            Thread.sleep((long) r.nextFloat()*3);
//        }
        findFoled(postid);
        System.out.println("success! num: "+start);
    }
    //输出内容
    public void printContent(String line,String postid) throws IOException {
        List<String> contentList = new ArrayList<>();
        //查找关键字
        String temp = "\"reply\"";
        String stoptemp = "\"stat\"";
        String secomment = "\"sub_replies\":[{";
        String sestopcomment = "\"r_user\":null";

        while (line.contains(secomment)) {
            int secomentIndex = line.lastIndexOf(secomment);
            int quoteIndex = line.indexOf(sestopcomment, secomentIndex+1); // 查找第一个双引号
            String str = line.substring(secomentIndex,quoteIndex+sestopcomment.length());
            line = line.replace(str,"");
            //System.out.println("index: "+secomentIndex);
        }

        while (line.contains(temp)) {
            int startIndex = line.lastIndexOf(temp) + temp.length(); // 跳过"content"字符串
            int stopIndex = line.indexOf(stoptemp, startIndex + 1); // 查找第一个"stat"
            String contentValue = line.substring(startIndex - temp.length(), stopIndex);
            contentList.add(contentValue);
            line = line.substring(0,startIndex - temp.length() - 2);
        }
//        if(contentList.size()<30){
//            writeFile("原数据:"+line+"拆分后:"+contentList.toString()+"\n","C:\\Users\\jizhaoyang\\Desktop\\test.txt");
//        }
        List<String> compares = new ArrayList<String>();
        compares.add("\"ip_region\"");
        compares.add("\"level\"");
        compares.add("\"nickname\"");
        compares.add("\"created_at\"");
        compares.add("\"floor_id\"");
        compares.add("\"content\"");
        compares.add("\"uid\"");
        for(String str : contentList){
            List<String> discuss = new ArrayList<>();
            for(String temp1:compares) {
                if (str != null && str.contains(temp1)) {
                    int startIndex = str.lastIndexOf(temp1) + temp1.length(); // 跳过"content"字符串
                    if(temp1.equals("\"level\"") || temp1.equals("\"created_at\"") || temp1.equals("\"floor_id\"")){
                        int secondQuoteIndex = str.indexOf(",", startIndex + 1); // 查找第一个逗号
                        if (secondQuoteIndex != -1) {
                            // 提取两个双引号之间的内容
                            String contentValue = str.substring(startIndex + 1, secondQuoteIndex);
                            // 提取关键字加两个双引号之间的内容
                            //String contentValue = str.substring(startIndex - temp1.length(), secondQuoteIndex);
                            discuss.add(contentValue);
                        }
                    }else {
                        int quoteIndex = str.indexOf("\"", startIndex); // 查找第一个双引号
                        if (quoteIndex != -1) {
                            int secondQuoteIndex = str.indexOf("\"", quoteIndex + 1); // 查找第二个双引号
                            if (secondQuoteIndex != -1) {
                                // 提取两个双引号之间的内容
                                String contentValue = str.substring(quoteIndex + 1, secondQuoteIndex);
                                // 提取关键字加两个双引号之间的内容
                                //String contentValue = str.substring(startIndex - temp1.length(), secondQuoteIndex);
                                discuss.add(contentValue);
                            }
                        }
                    }
                    str = str.substring(0, startIndex - temp1.length() - 2);
                }
            }
            Users user = new Users();
            Ip ip = new Ip();
            Discuss discuss1 = new Discuss();

            ip.setIpcol(discuss.get(0));
            ip.setIdip(discuss.get(6));

            user.setLevel(Integer.parseInt(discuss.get(1)));
            user.setNickname(discuss.get(2));
            user.setUid(discuss.get(6));
            user.setFinallytime(discuss.get(3));

            discuss1.setDiscusscol(discuss.get(5));
            discuss1.setIddis(discuss.get(6));
            discuss1.setTime(discuss.get(3));

//            System.out.println(user);
//            System.out.println(ip);
//            System.out.println(discuss1);
            System.out.println("Now: " + i +" "+contentList.size());
            writeFile(discuss.toString(), "C:\\Users\\jizhaoyang\\Desktop\\test.txt");
            i++;
            userService.tianjia(user);
            ipService.tianjia(ip);
            discussService.tianjia(discuss1);

            //遍历二级评论
            try {
                findsecoment(discuss.get(4),postid);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    //遍历二级评论
    public void findsecoment(String secId,String postid) throws IOException, InterruptedException {
        //第一次 https://bbs-api.miyoushe.com/post/wapi/getSubReplies?floor_id=194&gids=2&post_id=55489190&size=20
        //第二次 https://bbs-api.miyoushe.com/post/wapi/getSubReplies?floor_id=16607&gids=2&last_id=1818366894839582721&post_id=55489190&size=20
        //最新   https://bbs-api.miyoushe.com/post/wapi/getFoldedPostReplies?gids=2&is_hot=false&only_master=false&order_type=2&post_id=56494236&size=20
        boolean islast = false;
        String before = "\"list\"";
        String start = "";
        String size = "40";
        String url = "https://bbs-api.miyoushe.com/post/wapi/getSubReplies?floor_id="+secId+"&gids=2&post_id="+postid+"&size="+size;
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Accept", "application/json;charset=utf-8")  // 修改为期望的 JSON 类型\
                .header("cookie", "_MHYUUID=854f8953-a7df-438f-81d9-dc32f84d4a44; DEVICEFP_SEED_ID=8845fe6b87a20d05; DEVICEFP_SEED_TIME=1723053151120; _ga=GA1.1.601156800.1723053151; DEVICEFP=38d7fca8229de; _ga_KS4J8TXSHQ=GS1.1.1723212938.4.1.1723214160.0.0.0; acw_tc=0a32503917232151905613676e2aa570adca667897662f3944d58643654803")
                .GET()
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
        String responseBody = response.body();

        String lasttemp = "\"is_last\"";
        int lastIndex = responseBody.lastIndexOf(lasttemp) + lasttemp.length(); // 跳过"content"字符串
        int nextlastIndex = responseBody.indexOf(",", lastIndex + 1); // 查找第一个逗号
        if (nextlastIndex != -1) {
            // 提取之间的内容
            String contentValue = responseBody.substring(lastIndex + 1, nextlastIndex);
            islast= !contentValue.equals("false");
        }

        String lastid = "\"last_id\"";
        int lastidIndex = responseBody.lastIndexOf(lastid) + lastid.length(); // 跳过"last_id"字符串
        int quoteIndex = responseBody.indexOf("\"", lastidIndex); // 查找第一个双引号
        if (quoteIndex != -1) {
            int secondQuoteIndex = responseBody.indexOf("\"", quoteIndex + 1); // 查找第二个双引号
            if (secondQuoteIndex != -1) {
                // 提取两个双引号之间的内容
                start = responseBody.substring(quoteIndex + 1, secondQuoteIndex);
                writeFile("second--last_id: "+start+" "+islast+"\n","C:\\Users\\jizhaoyang\\Desktop\\text.txt");
            }
        }

        int beforeIndex = responseBody.lastIndexOf(before) + before.length();
        //System.out.println(beforeIndex+" "+(lastidIndex-lastid.length())+" "+responseBody.charAt(beforeIndex + 1)+" "+responseBody.charAt(lastidIndex-lastid.length()));
        responseBody = responseBody.substring(beforeIndex + 1,lastidIndex - lastid.length() - 1);

        printseByGson(responseBody);
        Random r = new Random();
        Thread.sleep((long) r.nextFloat()*3);
        while(!islast) {
            //https://bbs-api.miyoushe.com/post/wapi/getSubReplies?floor_id=16607&gids=2&last_id=1818366894839582721&post_id=55489190&size=20
            url = "https://bbs-api.miyoushe.com/post/wapi/getSubReplies?floor_id="+secId+"&last_id="+start+"&gids=2&post_id="+postid+"&size="+size;
            client = HttpClient.newHttpClient();
            request = HttpRequest.newBuilder()
                    .uri(URI.create(url))
                    .header("Accept", "application/json;charset=utf-8")  // 修改为期望的 JSON 类型\
                    .header("cookie", "_MHYUUID=854f8953-a7df-438f-81d9-dc32f84d4a44; DEVICEFP_SEED_ID=8845fe6b87a20d05; DEVICEFP_SEED_TIME=1723053151120; _ga=GA1.1.601156800.1723053151; DEVICEFP=38d7fca8229de; _ga_KS4J8TXSHQ=GS1.1.1723212938.4.1.1723214160.0.0.0; acw_tc=0a32503917232151905613676e2aa570adca667897662f3944d58643654803")
                    .GET()
                    .build();


            response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
            responseBody = response.body();

            lasttemp = "\"is_last\"";
            lastIndex = responseBody.lastIndexOf(lasttemp) + lasttemp.length(); // 跳过"content"字符串
            nextlastIndex = responseBody.indexOf(",", lastIndex + 1); // 查找第一个逗号
            if (nextlastIndex != -1) {
                // 提取之间的内容
                String contentValue = responseBody.substring(lastIndex + 1, nextlastIndex);
                islast= !contentValue.equals("false");
            }

            lastid = "\"last_id\"";
            lastidIndex = responseBody.lastIndexOf(lastid) + lastid.length(); // 跳过"last_id"字符串
            quoteIndex = responseBody.indexOf("\"", lastidIndex); // 查找第一个双引号
            if (quoteIndex != -1) {
                int secondQuoteIndex = responseBody.indexOf("\"", quoteIndex + 1); // 查找第二个双引号
                if (secondQuoteIndex != -1) {
                    // 提取两个双引号之间的内容
                    start = responseBody.substring(quoteIndex + 1, secondQuoteIndex);
                    writeFile("second--last_id: "+start+" "+islast+"\n","C:\\Users\\jizhaoyang\\Desktop\\text.txt");
                }
            }

            beforeIndex = responseBody.lastIndexOf(before) + before.length();
            //System.out.println(beforeIndex+" "+(lastidIndex-lastid.length())+" "+responseBody.charAt(beforeIndex + 1)+" "+responseBody.charAt(lastidIndex-lastid.length()));
            responseBody = responseBody.substring(beforeIndex + 1,lastidIndex - lastid.length() - 1);

            //printseContent(responseBody);
            printseByGson(responseBody);
            r = new Random();
            Thread.sleep((long) r.nextFloat()*3);
        }
    }
    //输出二级内容
    public void printseContent(String line) throws IOException {
        List<String> contentList = new ArrayList<>();
        //查找关键字
        String temp = "\"reply\"";
        String stoptemp = "\"stat\"";

        while (line.contains(temp)) {
            int startIndex = line.lastIndexOf(temp) + temp.length(); // 跳过"content"字符串
            int stopIndex = line.indexOf(stoptemp, startIndex + 1); // 查找第一个"stat"
            String contentValue = line.substring(startIndex - temp.length(), stopIndex);
            contentList.add(contentValue);
            line = line.substring(0,startIndex - temp.length() - 2);
        }
//        if(contentList.size()<30){
//            writeFile("原数据:"+line+"拆分后:"+contentList.toString()+"\n","C:\\Users\\jizhaoyang\\Desktop\\test.txt");
//        }
        List<String> compares = new ArrayList<String>();
        compares.add("\"ip_region\"");
        compares.add("\"level\"");
        compares.add("\"nickname\"");
        compares.add("\"created_at\"");
        compares.add("\"content\"");
        compares.add("\"uid\"");
        for(String str : contentList){
            List<String> discuss = new ArrayList<>();
            for(String temp1:compares) {
                if (str != null && str.contains(temp1)) {
                    int startIndex = str.lastIndexOf(temp1) + temp1.length(); // 跳过"content"字符串
                    if(temp1.equals("\"level\"") || temp1.equals("\"created_at\"")){
                        int secondQuoteIndex = str.indexOf(",", startIndex + 1); // 查找第一个逗号
                        if (secondQuoteIndex != -1) {
                            // 提取两个双引号之间的内容
                            String contentValue = str.substring(startIndex + 1, secondQuoteIndex);
                            // 提取关键字加两个双引号之间的内容
                            //String contentValue = str.substring(startIndex - temp1.length(), secondQuoteIndex);
                            discuss.add(contentValue);
                        }
                    }else {
                        int quoteIndex = str.indexOf("\"", startIndex); // 查找第一个双引号
                        if (quoteIndex != -1) {
                            int secondQuoteIndex = str.indexOf("\"", quoteIndex + 1); // 查找第二个双引号
                            if (secondQuoteIndex != -1) {
                                // 提取两个双引号之间的内容
                                String contentValue = str.substring(quoteIndex + 1, secondQuoteIndex);
                                // 提取关键字加两个双引号之间的内容
                                //String contentValue = str.substring(startIndex - temp1.length(), secondQuoteIndex);
                                discuss.add(contentValue);
                            }
                        }
                    }
                    str = str.substring(0, startIndex - temp1.length() - 2);
                }
            }
            Users user = new Users();
            Ip ip = new Ip();
            Discuss discuss1 = new Discuss();

            ip.setIpcol(discuss.get(0));
            ip.setIdip(discuss.get(5));

            user.setLevel(Integer.parseInt(discuss.get(1)));
            user.setNickname(discuss.get(2));
            user.setUid(discuss.get(5));
            user.setFinallytime(discuss.get(3));

            discuss1.setDiscusscol(discuss.get(4));
            discuss1.setIddis(discuss.get(5));
            discuss1.setTime(discuss.get(3));

//            System.out.println(user);
//            System.out.println(ip);
//            System.out.println(discuss1);
            System.out.println("Now: " + i +" "+contentList.size());
            writeFile(discuss.toString(), "C:\\Users\\jizhaoyang\\Desktop\\test.txt");
            i++;
            userService.tianjia(user);
            ipService.tianjia(ip);
            discussService.tianjia(discuss1);
        }
    }
    //遍历折叠评论
    public void findFoled(String postid) throws IOException, InterruptedException {
        //评论api https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=20&post_id=56356803&size=20
        //个人用户api https://bbs-api.miyoushe.com/post/wapi/userPost?size=10&uid=391147034  OR  https://bbs-api.miyoushe.com/post/wapi/userPost?offset=55249915&size=10&uid=391147034
        //评论url 上一次https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=20&post_id=56393124&size=20
        //       下一次 https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=40&post_id=56393124&size=20
        //   https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=60&post_id=56232502&size=20
        //   https://bbs-api.miyoushe.com/post/wapi/getPostReplies?gids=2&is_hot=true&last_id=40&post_id=55489190&size=20

        boolean islast = false;
        String before = "\"list\"";
        String start = "0";
        String size = "40";
        while(!islast) {
            String url = "https://bbs-api.miyoushe.com/post/wapi/getFoldedPostReplies?gids=2&is_hot=true&last_id="+start+"&only_master=false&post_id="+postid+"&size="+size;
            HttpClient client = HttpClient.newHttpClient();
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(url))
                    .header("Accept", "application/json;charset=utf-8")  // 修改为期望的 JSON 类型\
                    .header("cookie", "_MHYUUID=854f8953-a7df-438f-81d9-dc32f84d4a44; DEVICEFP_SEED_ID=8845fe6b87a20d05; DEVICEFP_SEED_TIME=1723053151120; _ga=GA1.1.601156800.1723053151; DEVICEFP=38d7fca8229de; _ga_KS4J8TXSHQ=GS1.1.1723212938.4.1.1723214160.0.0.0; acw_tc=0a32503917232151905613676e2aa570adca667897662f3944d58643654803")
                    .GET()
                    .build();


            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
            String responseBody = response.body();

            int beforeIndex = responseBody.lastIndexOf(before) + before.length();
            //System.out.println(beforeIndex+" "+(lastidIndex-lastid.length())+" "+responseBody.charAt(beforeIndex + 1)+" "+responseBody.charAt(lastidIndex-lastid.length()));
            String responsed = responseBody.substring(beforeIndex + 1,responseBody.length() - 2);

            responseBody = responseBody.substring(0,beforeIndex - before.length());

            String lastid = "\"last_id\"";
            int lastidIndex = responseBody.lastIndexOf(lastid) + lastid.length(); // 跳过"last_id"字符串
            int quoteIndex = responseBody.indexOf("\"", lastidIndex); // 查找第一个双引号
            if (quoteIndex != -1) {
                int secondQuoteIndex = responseBody.indexOf("\"", quoteIndex + 1); // 查找第二个双引号
                if (secondQuoteIndex != -1) {
                    // 提取两个双引号之间的内容
                    start = responseBody.substring(quoteIndex + 1, secondQuoteIndex);
                }
            }

            String lasttemp = "\"is_last\"";
            int lastIndex = responseBody.lastIndexOf(lasttemp) + lasttemp.length(); // 跳过"content"字符串
            int nextlastIndex = responseBody.indexOf(",", lastIndex + 1); // 查找第一个逗号
            if (nextlastIndex != -1) {
                // 提取之间的内容
                String contentValue = responseBody.substring(lastIndex + 1, nextlastIndex);
                islast= !contentValue.equals("false");
                writeFile("Folded--last_id: "+start+" "+islast+"\n","C:\\Users\\jizhaoyang\\Desktop\\text.txt");
            }

            printByGson(responsed,postid);
            //printContent(responseBody,postid);
            //findindex(responseBody);
            Random r = new Random();
            Thread.sleep((long) r.nextFloat()*3);
        }
        //System.out.println("success! num: "+start);
    }
    //查询序列测试
    public void findindex(String line){

        String temp = "\"reply\"";
        String secomment = "\"sub_replies\":[{";
        String sestopcomment = "\"r_user\":null";

        writeFile(line,"C:\\Users\\jizhaoyang\\Desktop\\text.txt");
        while (line.contains(secomment)) {
            int secomentIndex = line.lastIndexOf(secomment);
            int quoteIndex = line.indexOf(sestopcomment, secomentIndex+1); // 查找第一个双引号
            String str = line.substring(secomentIndex,quoteIndex+sestopcomment.length());
            line = line.replace(str,"");
            System.out.println("index: "+secomentIndex);
        }
        writeFile(line,"C:\\Users\\jizhaoyang\\Desktop\\test.txt");

//        while (line.contains(temp)) {
//            int startIndex = line.lastIndexOf(temp) + temp.length(); // 跳过"content"字符串
//            line = line.substring(0,startIndex - temp.length() - 2);
//        }
    }
    //使用gson解析输出
    public void printByGson(String json,String postid) throws IOException, InterruptedException {
        Gson gson = new Gson();
        // 注意：上面的Type是错误的，因为我们需要整个讨论项（包含Reply和User），所以应该调整如下：
        Type discussListType = new TypeToken<List<comments>>(){}.getType();
        List<comments> commentss = gson.fromJson(json, discussListType);
        // 现在你可以遍历discusses列表，并访问每个Discuss对象的uid和content
        for (comments comment : commentss) {
//            commentService.tianjia(comment);
//            System.out.println("Now: " + i);
//            writeFile("Now: " + i +" "+comment.getUser().getNickname()+" "+commentss.size()+"\n","C:\\Users\\jizhaoyang\\Desktop\\text.txt");
//            i++;
//            writeFile(comment.toString()+"\n","C:\\Users\\jizhaoyang\\Desktop\\file.txt");
            if(comment.getSub_replies().size() > 0){
                findsecoment(comment.getReply().getFloorid(),postid);
            }
        }
    }
    //使用gson解析二级评论输出
    public void printseByGson(String json) {
        Gson gson = new Gson();
        // 注意：上面的Type是错误的，因为我们需要整个讨论项（包含Reply和User），所以应该调整如下：
        Type discussListType = new TypeToken<List<comments>>(){}.getType();
        List<comments> commentss = gson.fromJson(json, discussListType);
        // 现在你可以遍历discusses列表，并访问每个Discuss对象的uid和content
        for (comments comment : commentss) {
            commentService.tianjia(comment);
            System.out.println("Now: " + i);
            writeFile("Now: " + i +" "+comment.getUser().getNickname()+" "+commentss.size()+"\n","C:\\Users\\jizhaoyang\\Desktop\\text.txt");
            i++;
            writeFile(comment.toString()+"\n","C:\\Users\\jizhaoyang\\Desktop\\file.txt");
        }
    }
}
